package com.cardvalue.sys.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.cardvalue.sys.R;
import com.cardvalue.sys.entity.Location;

import java.util.ArrayList;
import java.util.List;

/**
 * 搜索地址的adapter
 * Created by cardvalue on 2016/4/21.
 */
public class AddressRecylerAdapter extends RecyclerView.Adapter {
    private List<Location> mAddressList=new ArrayList<>();
    private Context context;
    private LayoutInflater inflater;
    public AddressRecylerAdapter( Context context, List<Location> mAddressList){
        this.context=context;
        this.mAddressList=mAddressList;
        this.inflater=LayoutInflater.from(context);
    }
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view=View.inflate(context, R.layout.listitem_address,null);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
       if(holder instanceof  ViewHolder){
           ViewHolder viewHolder=(ViewHolder) holder;
           Location address=mAddressList.get(position);
           if(address!=null){
               viewHolder.tv_city.setText(address.getCity()+address.getDistrict());
               viewHolder.tv_province.setText(address.getName());
           }
       }
    }

    @Override
    public int getItemCount() {
        return mAddressList!=null?mAddressList.size():0;
    }


    class ViewHolder extends  RecyclerView.ViewHolder{
        TextView  tv_city;//城市
        TextView tv_province;//
        public ViewHolder(View itemView){
            super(itemView);
            init(itemView);
            //给itemview设置点击事件
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(onItemClickListenter!=null){
                        onItemClickListenter.OnItemClick(v,getLayoutPosition());
                    }
                }
            });
        }


        private void init(View view){
            tv_city = (TextView) view.findViewById(R.id.tv_city);
            tv_province= (TextView) view.findViewById(R.id.tv_province);
        }
    }


    //对外暴漏点击事件的接口
    public interface OnItemClickListenter{
        void OnItemClick(View v,int position);
    }

    public OnItemClickListenter onItemClickListenter;

    public void setOnItemClickListenter(OnItemClickListenter onItemClickListenter){
        this.onItemClickListenter=onItemClickListenter;
    }

    public void updateDAta(List<Location> mAddressList) {
        this.mAddressList = mAddressList;
        notifyDataSetChanged();
    }
}
